package com.ynet.middleground.contract.utils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.springframework.stereotype.Component;

import com.deepoove.poi.data.CellRenderData;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.data.style.TableStyle;
import com.google.common.base.Throwables;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;

/**
 * 模板工具类
 *
 * @author yangbh
 */
@Component
public class TemplateUtil {

    public static final String SUFFIX = ".docx";

    public static String formatNumber(Object amount) {
        DecimalFormat decimalFormat = new DecimalFormat("###,###.00");
        return decimalFormat.format(amount);
    }

    public static CellRenderData amountCellRenderData(Object amount) {
        TableStyle tableStyle = new TableStyle();
        tableStyle.setAlign(STJc.RIGHT);
        // 数字格式化
        TextRenderData textRenderData = new TextRenderData(formatNumber(amount));
        return new CellRenderData(textRenderData, tableStyle);
    }

    public static CellRenderData textCentCellRenderData(String text) {
        TableStyle tableStyle = new TableStyle();
        tableStyle.setAlign(STJc.CENTER);
        // 文本
        TextRenderData textRenderData = new TextRenderData(text);
        return new CellRenderData(textRenderData, tableStyle);
    }

    public static File getFileByUrl(String fileUrl, String suffix) {
        IfpLogger.info(Thread.currentThread().getStackTrace()[1].getMethodName(), "{}文件下载++++++++++++++++++++Start",
            fileUrl);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        BufferedOutputStream stream = null;
        InputStream inputStream = null;
        File file = null;
        try {
            URL imageUrl = new URL(fileUrl);
            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            inputStream = conn.getInputStream();

            byte[] buffer = new byte[1024];
            int len = 0;

            while ((len = inputStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, len);
            }

            file = File.createTempFile("pattern", suffix);
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fileOutputStream);
            stream.write(outStream.toByteArray());

        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "读取文件异常, case: {}",
                Throwables.getStackTraceAsString(e));
            throw new BusinessException("读取文件异常", "ECCT0006");
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (stream != null) {
                    stream.close();
                }
                outStream.close();
            } catch (Exception e) {
            }
        }
        IfpLogger.info(Thread.currentThread().getStackTrace()[1].getMethodName(), "{}文件下载++++++++++++++++++++End",
            fileUrl);
        return file;
    }

}
